﻿<%@ WebHandler Language="C#" Class="TestHandler" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using Newtonsoft.Json;
using System.Web.Services;

public class TestHandler : IHttpHandler, IRequiresSessionState
{
    
    
    public void ProcessRequest (HttpContext context) {

        try
        {
            var action = context.Request["action"].ToString();
            switch (action)
            {
                case "saveImgBase64":
                    SaveImgBase64(context);
                    break;
                case "getImgBase64":
                    GetImgBase64(context);
                    break;
                case "test":
                    Test(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";

            context.Response.Write(JsonConvert.SerializeObject(result));
        }
     
        
      
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    void Test(HttpContext context)
    {
        var result = new uMESProcessResult();
        result.Result = false;
        context.Response.ContentType = "text/html";

        var ws = new WebService();
         
        
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 保存签名图片
    /// </summary>
    /// <param name="context"></param>
    public void SaveImgBase64(HttpContext context)
    {
        var result = new uMESProcessResult();
        result.Result = false;
        context.Response.ContentType = "text/html";
        
        //存为blob
        var request = context.Request;
        var strSql = @"insert into wjhtesttable(BLOBFILED,name) values(:BLOBFILED,:name)";
        var para = new OracleParameter[2];

        var base64Img = request["base64Img"].ToString();
        para[0] = new OracleParameter(":BLOBFILED", System.Text.Encoding.Default.GetBytes(base64Img));
        para[0].OracleType = OracleType.Blob;

        para[1] = new OracleParameter(":name", request["name"].ToString());
        para[1].OracleType = OracleType.VarChar;
        DatabaseHelper.ExecuteSql(strSql, para);

        result.Result = true;
        result.Message = "保存成功";
        
        context.Response.Write(JsonConvert.SerializeObject(result));
        
        
    }
    
     /// <summary>
    /// 读取签名图片
    /// </summary>
    /// <param name="context"></param>
    public void GetImgBase64(HttpContext context)
    {
        var result = new uMESProcessResult();
        result.Result = false;
        context.Response.ContentType = "text/html";
        
        var name = context.Request["name"].ToString();
        //读取blob
        var strSql = string.Format("select * from wjhtesttable where name='{0}'", name);
        DataTable dt = DatabaseHelper.Query_ReturnDatTable(strSql);
        dt.Columns.Add("STRIMG");

        foreach (DataRow dr in dt.Rows)
        {
            byte[] b = (byte[])dr["BLOBFILED"];
            dr["STRIMG"] = System.Text.Encoding.Default.GetString(b);
        }
        dt.AcceptChanges();

        result.ReturnData = dt;
        result.Result = true;
        context.Response.Write(JsonConvert.SerializeObject(result));
        
    }

}